const mysql = require('mysql2/promise');

// 数据库连接配置
const dbConfig = {
  host: 'localhost',
  port: 3306,
  user: 'root',
  password: 'newhand',
  database: 'hunaction',
  charset: 'utf8mb4',
  timezone: '+08:00'
};

// 乡镇街道数据
const townshipsData = [
  // 街道
  { name: '灵泉街道', code: '001', sort_order: 1 },
  { name: '秀江街道', code: '002', sort_order: 2 },
  { name: '湛郎街道', code: '003', sort_order: 3 },
  { name: '珠泉街道', code: '004', sort_order: 4 },
  { name: '化成街道', code: '005', sort_order: 5 },
  { name: '官园街道', code: '006', sort_order: 6 },
  { name: '下浦街道', code: '007', sort_order: 7 },
  { name: '凤凰街道', code: '008', sort_order: 8 },
  { name: '金园街道', code: '009', sort_order: 9 },
  { name: '新康府街道', code: '010', sort_order: 10 },
  
  // 镇
  { name: '彬江镇', code: '011', sort_order: 11 },
  { name: '西村镇', code: '012', sort_order: 12 },
  { name: '金瑞镇', code: '013', sort_order: 13 },
  { name: '温汤镇', code: '014', sort_order: 14 },
  { name: '三阳镇', code: '015', sort_order: 15 },
  { name: '慈化镇', code: '016', sort_order: 16 },
  { name: '天台镇', code: '017', sort_order: 17 },
  { name: '洪塘镇', code: '018', sort_order: 18 },
  { name: '芦村镇', code: '019', sort_order: 19 },
  { name: '水江镇', code: '020', sort_order: 20 },
  { name: '渥江镇', code: '021', sort_order: 21 },
  { name: '新坊镇', code: '022', sort_order: 22 },
  { name: '南庙镇', code: '023', sort_order: 23 },
  { name: '竹亭镇', code: '024', sort_order: 24 },
  { name: '辽市镇', code: '025', sort_order: 25 },
  { name: '寨下镇', code: '026', sort_order: 26 },
  { name: '湖田镇', code: '027', sort_order: 27 },
  { name: '新田镇', code: '028', sort_order: 28 },
  { name: '洪江镇', code: '029', sort_order: 29 },
  
  // 乡
  { name: '楠木乡', code: '030', sort_order: 30 },
  { name: '柏木乡', code: '031', sort_order: 31 },
  { name: '飞剑潭乡', code: '032', sort_order: 32 }
];

async function updateRegions() {
  let connection;
  
  try {
    console.log('🔄 连接数据库...');
    connection = await mysql.createConnection(dbConfig);
    console.log('✅ 数据库连接成功');
    
    // 清空现有数据
    console.log('🔄 清空现有地区数据...');
    await connection.execute('DELETE FROM regions WHERE 1=1');
    await connection.execute('ALTER TABLE regions AUTO_INCREMENT = 1');
    console.log('✅ 现有数据已清空');
    
    // 插入新数据
    console.log('🔄 插入新的乡镇街道数据...');
    for (const township of townshipsData) {
      await connection.execute(
        'INSERT INTO regions (parent_id, name, code, level, sort_order) VALUES (?, ?, ?, ?, ?)',
        [0, township.name, township.code, 1, township.sort_order]
      );
    }
    
    // 验证数据
    const [rows] = await connection.execute('SELECT COUNT(*) as total FROM regions');
    console.log(`✅ 数据插入完成，共插入 ${rows[0].total} 条记录`);
    
    // 显示部分数据
    const [sampleData] = await connection.execute('SELECT * FROM regions ORDER BY sort_order LIMIT 5');
    console.log('📋 示例数据:');
    sampleData.forEach(row => {
      console.log(`  - ${row.name} (${row.code})`);
    });
    
    console.log('🎉 地区数据更新完成！');
    
  } catch (error) {
    console.error('❌ 更新失败:', error.message);
    process.exit(1);
  } finally {
    if (connection) {
      await connection.end();
      console.log('🔌 数据库连接已关闭');
    }
  }
}

// 执行更新
updateRegions(); 